package pl.leader100.rest.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import simplemysql.SimpleMySQL;

public class DocDaoSAV {

	private static DocDaoSAV instance;

	public static DocDaoSAV GetInstance() {
		if (instance == null) {
			instance = new DocDaoSAV();
		}
		return instance;
	}

	private Map<String, Doc> contentProvider = new HashMap<String, Doc>();

	public List<Doc> getDocs(String year, String course, String role,
			String date, String locale) {
		role = role.toUpperCase();
		locale = locale.toLowerCase();

		ArrayList<Doc> list = new ArrayList<Doc>();
		// Calendar cal = Calendar.getInstance(new Locale(locale));
		DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
		Date dat;

		SimpleMySQL mysql = new SimpleMySQL();
		mysql.connect("l100", "l100", "warto");

		ResultSet result;
		String sql = "SELECT document.* FROM schedule as a, document as b WHERE a.document = b.iddocument AND a.role='"
				+ role
				+ "' AND a.date1<={"
				+ date
				+ "} AND a.date2<={"
				+ date
				+ "} AND a.year='" + year + "' AND a.course='" + course + "';";
		result = mysql.query(sql);

		Doc doc = null;
		try {
			while (result.next()) {
				doc = new Doc(result.getString("title"), 
						result.getString("path"), 
						result.getString("locale"), 
						df.format(result.getDate("date1")),
						df.format(result.getDate("date2")), 
						result.getString("mimetype"));
				list.add(doc);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}

	private DocDaoSAV() {
		Calendar date1 = Calendar.getInstance();
		date1.set(Calendar.HOUR_OF_DAY, 0);
		date1.set(Calendar.MINUTE, 0);
		date1.set(Calendar.SECOND, 1);
	
		Calendar date2 = Calendar.getInstance();
		date2.set(Calendar.MONTH, date1.get(Calendar.MONTH)+1);
		date2.set(Calendar.HOUR_OF_DAY, 23);
		date2.set(Calendar.MINUTE, 59);
		date2.set(Calendar.SECOND, 59);
	
		DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
	
		Doc doc = new Doc("student", "planroku", "plandorku.pdf", "pl",
				df.format(date1.getTime()), df.format(date2.getTime()));
		contentProvider.put(doc.getPath(), doc);
	
		doc = new Doc("teacher", "conseils", "conseils.pdf", "pl",
				df.format(date1.getTime()), df.format(date2.getTime()));
		contentProvider.put(doc.getPath(), doc);
	}

	// public Doc retrieveDoc(String path) {
	// return getModel().get(path);
	// }

	public String retrieveDoc(String path) {
		return "http://l100.wegielek.edu.pl/doc/"
				+ getModel().get(path).getLocale() + "/"
				+ getModel().get(path).getPath();
	}

	/**
	 * return a list of doc fitting to the sent param
	 * 
	 * @param role
	 * @param date
	 * @param locale
	 * @return
	 */
	public List<Doc> getDocs(String role, String date, String locale) {
		role = role.toUpperCase();
		locale = locale.toLowerCase();

		ArrayList<Doc> list = new ArrayList<Doc>();
		// Calendar cal = Calendar.getInstance(new Locale(locale));
		DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
		Date dat;
		for (Doc doc : getModel().values()) {

			//try {
				if (date.equals("*"))
					dat = null;
				//else
					//dat = df.parse(date);

		/*		if ((doc.getRole().toUpperCase().equals(role) || "*"
						.equals(role))
						&& (doc.getLocale().toLowerCase().equals(locale) || "*"
								.equals(locale))
						&& ((dat == null)
								|| (dat.after(df.parse(doc.getDate1()))) || (df
								.format(dat).equals(doc.getDate1())))
						&& ((dat == null)
								|| (dat.before(df.parse(doc.getDate2()))) || (df
								.format(dat).equals(doc.getDate2())))) { */

					list.add(doc);
		/*		}
			} catch (ParseException e) {
				e.printStackTrace();
			} */
		}
		return list;
	}

	public Map<String, Doc> getModel() {
		return contentProvider;
	}
}
